package renda.action.user;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import renda.common.ImageCodeUtil;
import renda.common.SessionUtil;

public class ImageCodeAction {
	private InputStream image;
	
	public String execute(){
		Map<String, BufferedImage> map = ImageCodeUtil.getImage();
		String code = map.keySet().iterator().next();
		BufferedImage bi = map.get(code);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
		try {
			encoder.encode(bi);
			image = new ByteArrayInputStream(baos.toByteArray());
			SessionUtil.setAttr("currcode", code);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	public void setImage(InputStream image) {
		this.image = image;
	}
	public InputStream getImage() {
		return image;
	}
}
